﻿//-----------------------------------------------------------------------
// <copyright file="RollbackAttribute.cs" company="Transilvania University of Brasov">
//     Copyright (c) Nagy Robert. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Transactions;
using NUnit.Framework;
using NUnit.Framework.Interfaces;

/// <summary>
/// Rollback Attribute wraps test execution into a transaction and cancels the transaction once the test is finished.
/// You can use this attribute on single test methods or test classes/suites
/// </summary>
public class RollbackAttribute : Attribute, ITestAction
{
    /// <summary>
    /// The transaction
    /// </summary>
    private TransactionScope transaction;

    /// <summary>
    /// Gets the target for the action attribute
    /// </summary>
    public ActionTargets Targets
    {
        get { return ActionTargets.Test; }
    }

    /// <summary>
    /// Executed before each test is run
    /// </summary>
    /// <param name="test">The test that is going to be run.</param>
    public void BeforeTest(ITest test)
    {
        this.transaction = new TransactionScope();
    }

    /// <summary>
    /// Executed after each test is run
    /// </summary>
    /// <param name="test">The test that has just been run.</param>
    public void AfterTest(ITest test)
    {
        this.transaction.Dispose();
    }
}